package Tools;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

import parser.cfg;
import test2.Main;
public class greyLang {
	public static boolean isString(String text) {
		if(text.substring(0,1).equals("\"") && text.substring(text.length()-1,text.length()).equals("\"")) {
			return true;
		}
		return false;
	}
	public static String test_printWord(ArrayList<parser.Word> word) {
		String text="\r\n[";
		for(int i=0;i<word.size();i++) {
			String temp="\t{\"value\":"+packString(word.get(i).text)+",\"type\":"+packString(test_getWordType(word.get(i).type))+",\"line\":"+word.get(i).line+"}";
			if(i==word.size()-1) {
				text=text+"\r\n"+temp;
				continue;
			}else {
				text=text+"\r\n"+temp+",";
			}
		}
		text=text+"\r\n]\r\n";
		return text;
	}
	public static String test_printVar(Map<String,test2.Var> varList) {
		String text="[";
		int i=0;
		for (Map.Entry<String, test2.Var> entry : varList.entrySet()) { 
			String temp="{name:"+entry.getKey()+",type:"+entry.getValue().type+",value:"+entry.getValue().value+"}";
			if(i==0) {
				text=text+temp;
			}else {
				text=text+","+temp;
			}
			i++;
			continue;
		}
		text=text+"]";
		return text;
	}
	public static String test_getWordType(int type) {
		if(type==cfg.WORD_ATEXT) {
			return "标识符";
		}
		if(type==cfg.WORD_IMP) {
			return "关键字";
		}
		if(type==cfg.WORD_NUM) {
			return "数字";
		}
		if(type==cfg.WORD_TEXT) {
			return "字符串";
		}
		if(type==cfg.WORD_U) {
			return "保留的特殊符号";
		}
		return "未知";
	}
	public static boolean isImportantWord(String text) {
		for(int i=0;i<cfg.important_word.length;i++) {
			if(text.equals(cfg.important_word[i])) {
				return true;
			}
		}
		return false;
	}
	public static void throwErr(String text,int line) {
		if(line==-1) {
			System.err.println("错误:"+text+".无法定位这个错误");
		}else {
			System.err.println("错误:"+text+"。在 "+line+" 行.");
		}
		System.exit(0);
	}
	public static void writeLog(String log) throws IOException {
		FileWriter out = new FileWriter(Main.logPath,true);
		out.write(log+"\r\n");
		out.flush();
		out.close();
	}
	public static String packString(String text) {
		String part1=text.replace("\"","\\\"");
		return "\""+part1+"\"";
	}
}
